//
//  GQHTransactionDetailController.m
//  Xanthium
//
//  Created by GuanQinghao on 28/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHTransactionDetailView.h"

#pragma mark Controller
#import "GQHTransactionDetailController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHTransactionDetailController () <GQHTransactionDetailViewDelegate> {
    
#pragma mark Data
    NSMutableDictionary *parameters;
#pragma mark View
    /// 自定义根视图
    GQHTransactionDetailView *rootView;
    /// 弹出框控制器
    UIAlertController *alertController;
}

@end


@implementation GQHTransactionDetailController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHTransactionDetailView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    rootView.qh_editable = _qh_editable;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
    if(_qh_contentDictionary) {
        
//        captionArray = @[@"转账日期",@"转出账号",@"账号户名",@"开户银行",@"转账币种",@"转账金额",@"转账备注"];
//
//        NSString *transactionDate = [_qh_contentDictionary objectForKey:@"transactionDate"];
//        NSString *accountNum = [_qh_contentDictionary objectForKey:@"accountNum"];
//        NSString *nameOfPayer = [_qh_contentDictionary objectForKey:@"nameOfPayer"];
//        NSString *draweeBank = [_qh_contentDictionary objectForKey:@"draweeBank"];
//        NSString *currency = [_qh_contentDictionary objectForKey:@"currency"];
//        CGFloat amount = [[_qh_contentDictionary valueForKey:@"amount"] floatValue];
//        NSString *note = [_qh_contentDictionary objectForKey:@"note"];
//
        NSMutableArray *contentArray = [NSMutableArray array];
        [contentArray insertObject:[_qh_contentDictionary objectForKey:@"transactionDate"] atIndex:0];
        [contentArray insertObject:[_qh_contentDictionary objectForKey:@"accountNum"] atIndex:1];
        [contentArray insertObject:[_qh_contentDictionary objectForKey:@"nameOfPayer"] atIndex:2];
        [contentArray insertObject:[_qh_contentDictionary objectForKey:@"draweeBank"] atIndex:3];
        [contentArray insertObject:[_qh_contentDictionary objectForKey:@"currency"] atIndex:4];
        [contentArray insertObject:[_qh_contentDictionary valueForKey:@"amount"] atIndex:5];
        [contentArray insertObject:[_qh_contentDictionary objectForKey:@"note"] atIndex:6];
        
        rootView.qh_contentArray = contentArray;
        [rootView.qh_tableView reloadData];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"转账详情";
    
    if (_qh_editable) {
        
        [self.qh_rightButton setTitle:@"提交" forState:UIControlStateNormal];
    } else {
        
        self.qh_rightButton.hidden = YES;
    }
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
            
        case 0: {
            
            /// 转账日期
            UIDatePicker *datePicker = [[UIDatePicker alloc] init];
            datePicker.datePickerMode = UIDatePickerModeDate;
            [datePicker addTarget:self action:@selector(datePickerChangeValue:) forControlEvents:UIControlEventValueChanged];
            __weak typeof(datePicker) weakDatePicker = datePicker;
            
            alertController = [UIAlertController alertControllerWithTitle:@"请选择转账日期" message: nil preferredStyle:UIAlertControllerStyleAlert];
            [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                
                textField.placeholder = @"请选择转账日期";
                textField.tag = 900;
                textField.inputView = weakDatePicker;
            }];
            
            [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"transactionDate"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:0];
                    [rootView.qh_tableView reloadData];
                }
            }]];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
            break;
        case 1: {
            
            /// 转出账号
            [self qh_alertWithTitle:@"请输入转出账号" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入转出账号";
                textField.keyboardType = UIKeyboardTypeNumberPad;
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"accountNum"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:1];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 2: {
            
            /// 账号户名
            [self qh_alertWithTitle:@"请输入账号户名" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入账号户名";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                [parameters setObject:inputTextField.text forKey:@"nameOfPayer"];
                [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:2];
                [rootView.qh_tableView reloadData];
            } completion:nil];
        }
            break;
        case 3: {
            
            /// 开户银行
            [self qh_alertWithTitle:@"请输入开户银行" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入开户银行";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"draweeBank"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:3];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 4: {
            
            /// 转账币种
        }
            break;
        case 5: {
            
            /// 转账金额
            [self qh_alertWithTitle:@"请输入转账金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入转账金额";
                textField.keyboardType = UIKeyboardTypeNumberPad;
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"amount"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:5];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 6: {
            
            /// 转账备注
            [self qh_alertWithTitle:@"请输入转账备注" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入转账备注";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"note"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:6];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark --TargetMethod
-(void)qh_clickedRightButton:(UIButton *)sender {
    
    NSArray * keys = @[
                       @"transactionDate",
                       @"accountNum",
                       @"nameOfPayer",
                       @"draweeBank",
                       @"",
                       @"amount",
                       @"note"];
    for (int i = 0; i < keys.count; i ++)
    {
        if (i != 4)
        {
            [parameters setObject:rootView.qh_contentArray[i] forKey:keys[i]];
        }
    }
    
    [parameters setValue:USER_GET_ID forKey:@"userId"];
    [parameters setValue:@"01" forKey:@"currency"];
    [parameters setValue:_qh_inBankID forKey:@"gatherInfoId"];
    
    if (parameters.count < 9) {
        
        [self showToastWithText:@"转账信息填写不完整"];
    } else {
        
        /// 添加转账
        NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buOfflineRechargeRecord/1/save"];
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (0 == [code intValue]) {
                DLog(@"%@---%@",message,data);
                // 返回上两级页面
                NSUInteger index = [[self.navigationController viewControllers] indexOfObject:self];
                UIViewController *controller = [[self.navigationController viewControllers] objectAtIndex:index - 2];
                [self.navigationController popToViewController:controller animated:YES];
            } else {
                
                [self showToastWithText:message];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
        }];
    }
}

#pragma mark --PrivateMethod
- (IBAction)datePickerChangeValue:(UIDatePicker *)sender {
    
    NSDate *date = sender.date;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    UITextField *inputTextField = (UITextField *)[alertController.view viewWithTag:900];
    NSString *dateString = [formatter stringFromDate:date];
    inputTextField.text = dateString;
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
